package com.bw.common.mvvm.model;

import com.bw.common.entity.BaseEntity;
import com.bw.common.entity.ErroEntity;
import com.bw.common.field.ErroCode;
import com.bw.common.field.ErroMsg;
import com.bw.common.network.RXFunction;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import io.reactivex.Observable;
import okhttp3.MediaType;
import okhttp3.RequestBody;

public abstract class BaseModel implements IModel{
    @Override
    public RequestBody createBody(Map<String, Object> map) {
        if (map==null){
            return RequestBody.create(MediaType.parse("application/json"),"");
        }
        return RequestBody.create(MediaType.parse("application/json"),
                new Gson().toJson(map));
    }

    @Override
    public Observable<BaseEntity> changeObservable(Observable parames) {
        return parames.map(new RXFunction());
    }

    @Override
    public Observable<BaseEntity> mergeObservable(Observable... parames) {
        ErroEntity entity = new ErroEntity();
        entity.status = ErroCode.PARAMESCODE;
        entity.message = ErroMsg.PARAMESMSG;
        if (parames == null)
            return Observable.just(entity);
        if (parames.length == 0)
            return Observable.just(entity);
        List<Observable<BaseEntity>>list = new ArrayList<>();
        for (int i = 0;i < parames.length;i ++)
            list.add(parames[i]);
        return Observable.merge(list);
    }
}
